const query = require('../../utils/DB')
class indexModel {
	static getMenu() {
		return query(`select * from menu where status=1`)
	}

	static getad() {
		return query(`select * from ly_ad where status=1 and type=1`)
	}

	// 首页商品 
	static async getGoods(city = '北京市', start = 1, size = 10) {
		let nowPage = (start - 1) * size
		let num = await query(`select count(*) as nums from ly_goods where listing=1 and address like '%${city}%'`)
		let arr = await query(`select * from ly_goods where listing=1 and address like '%${city}%' limit ${nowPage},${size}`)
		return { data: arr, sum: num[0]['nums'] }
	}

	// 视频列表
	static async getVideo(start = 1, size = 8) {
		let nowPage = (start - 1) * size
		let num = await query(`select count(*) as nums from ly_video where flag=1`)
		let arr = await query(`select v.*,m.headerurl as avatar from ly_video as v,ly_member as m where v.mid=m.id and v.flag=1 limit ${nowPage},${size}`)
		return { data: arr, sum: num[0]['nums'] }
	}
	// 商品详情
	static async getGoodsDetail(id) {
		let res = await query(`select * from ly_goods where id=${id} and listing=1`)
		if (res.length == 0) {
			return false
		}
		let tagList = await query(`select * from ly_tag where gid =${id}`)
		let skuList = await query(`select * from ly_sku where gid=${id}`)
		let photoList = await query(`select * from ly_goods_photo where gid=${id}`)
		// let messageList = await query(`select * from message where gid=${id}`)
		return { ...res[0], tagList, skuList, photoList }
		// return {...res[0],tagList,skuList,messageList}
	}

	// 首页商品
	static async searchGoods(start = 1, size = 10, keyword = "") {
		let nowPage = (start - 1) * size
		let num = await query(`select count(*) as nums from ly_goods where listing=1 and goodsname like "%${keyword}%" `)
		let arr = await query(`select * from ly_goods where listing=1 and goodsname like "%${keyword}%" limit ${nowPage},${size}`)
		return { data: arr, sum: num[0]['nums'] }
		// return {data:[],sum:10}
	}
	// 点赞收藏
	static async videoEdit(type, uid, vid, vuser) {
		// 查询 当前用户 是否已经点赞或者收藏了 
		let res = await query(`select * from ly_videouser where type='${type}' and uid=${uid} and vid=${vid}`)
		if (res.length > 0) {
			if (type != 'zhuangfa') {
				await query(`delete from ly_videouser where id=${res[0].id}`)
				await query(`update ly_video set ${type}=${type}-1 where id=${vid}`)
				await query(`update ly_member set ${type}=${type}-1 where id=${vuser}`)
			} else {
				await query(`update ly_video set zhuangfa=zhuangfa+1 where id=${vid}`)
				// await query(`update ly_member set ${type}=${type}+1 where id=${vuser}`)
			}
		} else {
			await query(`update ly_video set ${type}=${type}+1 where id=${vid}`)
			await query(` INSERT INTO ly_videouser(uid, vid, type) VALUES (${uid}, ${vid}, '${type}')`)
			// await query(`update ly_member set ${type}=${type}+1 where id=${vuser}`)
		}
		return true
	}

	static async getVideoUser(vid,uid){
		console.log(`select * from ly_videouser where vid=${vid} and uid=${uid}`)
		return await query(`select * from ly_videouser where vid=${vid} and uid=${uid}`)
	}
}

module.exports = indexModel